package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.ThemeTagDestQuery;
import cn.wolfcode.luowowo.article.query.TravelQueryObject;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IDestinationStaticVORedisService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.anno.UserParm;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class DestController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;

    @Reference
    private IMouthService mouthService;

    @Reference
    private IThemeService themeService;

    @Reference
    private IThemeTagService themeTagService;

    @Reference
    private IScenicService scenicService;

    @Reference
    private IDestinationStaticVORedisService destinationStaticVORedisService;


    @RequestMapping("/destination/guide")
    public Object guide(Long id, Model model,@UserParm UserInfo userInfo) {
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest", dest);
        model.addAttribute("toasts", toasts);

        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.queryCatalogByDestId(id);
        model.addAttribute("catalogs", strategyCatalogs);

        List<StrategyDetail> strategyDetails = strategyDetailService.getViewNumTop3(id);
        model.addAttribute("strategyDetails", strategyDetails);


        //判断是否收藏
        if (userInfo != null){
            model.addAttribute("isFavor", destinationStaticVORedisService.isFavor(id,userInfo.getId()));
        }
        return "/destination/guide";
    }

    @RequestMapping("/destination/getHotDestByRegionId")
    public String getHotDestByRegionId(Long regionId, Model model) {
        List<Destination> list = destinationService.queryHotDestByRegionId(regionId);
        List<Destination> leftDests = list.subList(0, list.size() / 2 + 1);
        List<Destination> rightDests = list.subList(list.size() / 2 + 1, list.size());
        model.addAttribute("leftDests", leftDests);
        model.addAttribute("rightDests", rightDests);
        model.addAttribute("regionId", regionId);
        return "destination/hotdestTpl";
    }

    @RequestMapping("/destination")
    public String destination(Model model) {
        List<Region> hotRegions = regionService.queryHotRegions();
        model.addAttribute("hotRegions", hotRegions);


        //当季推荐,截取月份
        //todo
        List<Mouth> mouths = mouthService.list();
        List<Mouth> mouths1 = mouths.subList(1, 12);
        model.addAttribute("mou", mouths1);

        //主题精选
        //todo
        List<Theme> themes = themeService.list();
        model.addAttribute("themes", themes);
        return "destination/index";
    }


    /**
     * 通过月份查询目的地
     *
     * @param regionId
     * @param model
     * @return
     */
    //destination/getDestByMouth
    @RequestMapping("/destination/getDestByMouth")
    public Object getDestByMouth(Long regionId, Model model) {
        List<Destination> destinations = destinationService.selectDestByMouthId(regionId);
        model.addAttribute("destinations", destinations);
        model.addAttribute("regionId", regionId);
        return "/destination/mouthdestTpl";
    }


    /**
     * 主题精选
     *
     * @param regionId
     * @param model
     * @return
     */
    //destination/getDestByThemeId
    @RequestMapping("/destination/getDestByThemeId")
    public Object getDestByThemeId(Long regionId, Model model) {
        List<ThemeTag> themeTags = new ArrayList<>();
        if (regionId == Theme.THEME_DATA) {
            List<Mouth> mouths = mouthService.list();
            List<Mouth> mouthList = mouths.subList(12, mouths.size());
            for (Mouth mouth : mouthList) {
                ThemeTag tag = new ThemeTag();
                tag.setId(mouth.getId());
                tag.setCoverUrl(mouth.getCoverUrl());
                tag.setName(mouth.getMouth());
                tag.setTheme_id(Theme.THEME_DATA);
                themeTags.add(tag);
            }
        } else {
            themeTags = themeTagService.selectDestByThemeId(regionId);
        }
        model.addAttribute("themeTags", themeTags);
        model.addAttribute("regionId", regionId);


        return "/destination/themedestTpl";
    }


    /**
     * 目的地筛选
     *
     * @param qo
     * @return
     */
    //todo
    @RequestMapping("/destination/themetagDest")
    public String themetagDest(@ModelAttribute("qo") ThemeTagDestQuery qo, Model model) {

        //时间
        List<Mouth> mouthList = mouthService.list();
        List<Mouth> mouths = mouthList.subList(0, 12);
        List<Mouth> datas = mouthList.subList(12, mouthList.size());
        model.addAttribute("mouths", mouths);
        model.addAttribute("datas", datas);

        //主题全年适宜
        List<ThemeTag> years = themeTagService.selectDestByThemeId(Theme.THEME_YEAR);
        model.addAttribute("years", years);

        //季节
        List<ThemeTag> festivals = themeTagService.selectDestByThemeId(Theme.THEME_FESTIVAL);
        model.addAttribute("festivals", festivals);

        //出行方式
        List<ThemeTag> ways = themeTagService.selectDestByThemeId(Theme.THEME_WAY);
        model.addAttribute("ways", ways);


     /* //目的地查询
        PageInfo<Destination> pageInfo = destinationService.queryDestByTheme(qo);
        model.addAttribute("pageInfo", pageInfo);*/
        return "/destination/destFilter";
    }

    /**
     * ajax请求优化
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/destination/themetagDestTWO")
    public String themetagDestTWO(@ModelAttribute("qo") ThemeTagDestQuery qo, Model model) {
        //单一ajax查询目的地
        PageInfo<Destination> pageInfo = destinationService.queryDestByTheme(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/destination/desFilterTpl";
    }



        //surveyPage
    @RequestMapping("destination/surveyPage")
    public Object surveyPage(@ModelAttribute("qo") StrategyCatalogQuery qo, Model model) {
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest", dest);
        model.addAttribute("toasts", toasts);
        return "/destination/survey";
    }

    @RequestMapping("destination/survey")
    public Object survey(@ModelAttribute("qo") StrategyCatalogQuery qo, Model model) {
        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs", strategyCatalogs);
        //catalogs
        StrategyCatalog catalog = null;
        for (StrategyCatalog c : strategyCatalogs) {
            if (c.getId() == qo.getCatalogId()) {
                catalog = c;
                model.addAttribute("catalog", catalog);
            }
        }
        if (catalog != null) {
            List<StrategyDetail> details = catalog.getDetails();
            if (details != null && details.size() > 0) {
                StrategyDetail detail = details.get(0);
                detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
                model.addAttribute("detail", details.get(0));
            }
        }
        return "/destination/surveyTpl";
    }

    /**
     * 游记
     */
    @RequestMapping("/destination/travels")
    public Object travels(@ModelAttribute("qo") TravelQueryObject qo, Model model) {
        PageInfo<Travel> pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/destination/travelTpl";
    }


    /**
     * 地点收藏
     */
    @RequestMapping("/destination/favor")
    @ResponseBody
    public Object favor(@UserParm UserInfo userInfo, Long did){
        if (userInfo == null){
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.RESPONSE_CODE_NOLOGIN);
            return result;
        }
        //如果是true:表示收藏 false:取消收藏
        boolean ret = destinationStaticVORedisService.favor(userInfo.getId(),did);
        //收藏成功
        return new AjaxResult(ret,null).
                addData(destinationStaticVORedisService.isFavor(userInfo.getId(),did));
    }
}
